Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

247
Views
¿Cómo puedo saber si una fecha ISO como "2021-12-12" ocurrió en los últimos 3 días?

Estoy creando un sitio que obtiene datos de una API de speedrun.com y coloca las ejecuciones más recientes en el sitio en orden de tiempo. Recientemente agregué una función que se ejecuta desde el mismo día en que se ve el sitio y tendrá un ícono especial que muestra que son "NUEVOS". Esta es mi solución actual (Javascript):

 runs[i].date = "2021-12-12"; // The API does not return hours if (new Date(new Date().getTime() - new Date().getTimezoneOffset() * 60000).toJSON().slice(0, 10) === runs[i].date) { alert("Today"); }

Si la fecha fuera el 12 de diciembre de 2021, esto alertaría a Today .

Esta solución tiene en cuenta las diferencias de zona horaria y funciona bien. Sin embargo, me gustaría que esto sea posible para las ejecuciones que ocurrieron en los últimos 3 días, por lo que incluso si runs[i].date = "2021-12-11" (10 de diciembre) o runs[i].date = "2021-12-11" (11 de diciembre), aún alertaría Today .

¿Cómo se haría esto?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Dividirlo en funciones y componerlas:

 const DAY = 1000 * 60 * 60 * 24; function datesAreInRange (date1, date2, days) { const ms = days * DAY; const diff = Math.abs(date2.getTime() - date1.getTime()); return diff <= ms; } function getDateFromString (ymd) { const [y, m, d] = ymd.split('-').map(str => parseInt(str, 10)); return new Date(y, m - 1, d); } const today = '2021-12-16'; const otherDates = [ '2021-12-20', '2021-12-19', '2021-12-18', '2021-12-17', '2021-12-16', '2021-12-15', '2021-12-14', '2021-12-13', '2021-12-12', ]; for (const other of otherDates) { const inRange = datesAreInRange(...[today, other].map(getDateFromString), 3); console.log(`${other} is ${inRange ? '' : 'not '}in range.`) }

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!